#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include "testutils.h"

/*
 * Adventurer effect:
 *		Draw cards until two treasure cards are found.
 *		Place the treasure cards in your hand.
 *		Discard the other drawn cards.
 *		Cost: 6 Coins
 **/
void random_test_adventurer()
{
	int i, j;
	int prev_handcount;
	int prev_treasure_count_in_hand;
	int prev_adventurer_count;
	int fail_count = 0;
	struct gameState state;

	init_test_data(&state, 1);

	srand(time(NULL));

	for (i = 0; i < MAX_ITERATIONS; i++) {
		state.deckCount[0] = 50;
		for (j = 0; j < state.deckCount[0]; j++) {
			state.deck[0][j] = random_card();
		}
		
		if (treasure_count(state.deck[0], state.deckCount[0]) < 2) {
			printf("Iteration %d - not enough treasure in deck.  Skipping.\n", i);
			continue;
		}

		state.handCount[0] = 5;
		for (j = 0; j < 4; j++) {
			state.hand[0][j] = random_card();
		}
		state.hand[0][4] = adventurer;

		prev_treasure_count_in_hand = treasure_count(state.hand[0], state.handCount[0]);
		prev_handcount = state.handCount[0];
		prev_adventurer_count = card_count(state.hand[0], adventurer, state.handCount[0]);

		cardEffect(adventurer, 0, 0, 0, &state, 4, 0);

		if (state.handCount[0] != prev_handcount + 1) {
			printf("FAIL: iteration %d.  Hand count incorrect.\n", i);
			fail_count++;
		}

		if (treasure_count(state.hand[0], state.handCount[0]) != prev_treasure_count_in_hand + 2) {
			printf("FAIL: iteration %d.  Did not draw two new treasure cards.\n", i);
			fail_count++;
		}

		if (card_count(state.hand[0], adventurer, state.handCount[0]) >= prev_adventurer_count) {
			printf("FAIL: iteration %d.  Did not discard adventurer card used.\n", i);
			fail_count++;
		}
	}

	if (fail_count > 0) {
		printf("Test failed!  Number of failures: %d possible\n", fail_count);
	}
	else {
		printf("All tests passed!\n");
	}
}

int main(void)
{
	random_test_adventurer();
	return 0;
}